<?php
/*------------------------------------------------------ */
//--门店主页
//-- Author: iqgmy
/*------------------------------------------------------ */
namespace app\shop\controller;
use app\ClientbaseController;
use think\Db;
use think\Request;
use think\facade\Cache;
use app\shop\model\SlideModel;
use app\shop\model\GoodsModel;
use app\shop\model\NavMenuModel;
class Store  extends ClientbaseController{
	/*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
	public function index($isIndex = false){
//	    dump(input('keyword'));exit;
		//调用自定义首页
		if ($isIndex == false && settings('shop_index_tpl') == 1){
			return $this->shopIndex();
		}
		$tipsubscribe = 0;//是否显示提示关注
        //微信网页访问执行
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')){
            if ($this->userInfo['user_id'] > 0){
                $subscribe = (new \app\weixin\model\WeiXinUsersModel)->where('user_id',$this->userInfo['user_id'])->value('subscribe');
                $tipsubscribe = $subscribe == 1 ? 0 : 1;
            }
        }
        $address = $this->get_city();

        $address = json_decode($address,true);
        if(input('keyword')){
            $where[] = ['store_name','LIKE','%'.input('keyword').'%'];
        }else{
            if(input('city_id')){
                $where[] = ['city_id','=',input('city_id')];
                $city = Db::name('main_region')->where('id',input('city_id'))->value('name');
            }elseif($address["data"]['city_id']){
                $where[] = ['city_id','=',$address["data"]['city_id']];
            }
        }
        $city = $city ? $city : $address['data']['city'];
//        $wheres[] = ['status','=',1];
        $this->assign('tipsubscribe', $tipsubscribe);
		$this->assign('title', '首页');

		$this->assign('slideList', SlideModel::getRowsStore());//获取幻灯片
		$this->assign('navMenuList', NavMenuModel::getRowsStore());//获取导航菜单
		$GoodsModel = new GoodsModel();
		$list_ = Db::name('distribution_store')->where($where)->where('status',1)->select();

		$this->assign('classGoods',$GoodsModel->getIndexClass());//获取首页分类
		$this->assign('list_',$list_);//获取首页分类
		$this->assign('city',$city);//获取首页分类

//        dump($list_);exit;
//		$this->assign('bestGoods',$GoodsModel->getIndexBestGoods());//获取首页分类
		return $this->fetch('index');
	}
    /*------------------------------------------------------ */
    //-- ip获取城市
    /*------------------------------------------------------ */
    public  function get_city( $data = null){
//        $ip = request()->ip();
        $ip = '113.67.17.63';
        $url = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
	/*------------------------------------------------------ */
	//-- 分类页
	/*------------------------------------------------------ */
	public function allSort(){
		$this->assign('title', '分类');
		$GoodsModel = new GoodsModel();
		$this->assign('allSort',$GoodsModel->getClassToAllSort());//获取分层分类
		$this->assign('classList',$GoodsModel->getClassList());//获取分类
		return $this->fetch('all_sort');
	}
	/*------------------------------------------------------ */
	//-- 搜索页
	/*------------------------------------------------------ */
	public function search(){
		$this->assign('title', '搜索');
//		$this->assign('hotSearch', explode(' ',settings('hot_search')));
        $GoodsModel = new GoodsModel();
        $this->assign('searchKeys', $GoodsModel->searchKeys());
		return $this->fetch('search');
	}
	/*------------------------------------------------------ */
	//-- 自定义首页
	/*------------------------------------------------------ */
	protected function shopIndex(){
		
		$mkey = 'shopIndex_web';
		$theme = Db::table('shop_page_theme')->find();
		if (empty($theme)) return $this->index(true);
	    $body = Cache::get($mkey);
		if (empty($body)){
			$ShopPageTheme = new \app\shop\model\ShopPageTheme();
			$d_products = $ShopPageTheme->defPproducts();			
			$page = json_decode($theme['page'],true);
			$GoodsModel = new GoodsModel();
			foreach ($page['pageElement'] as $key=>$row){
				$_body = '';
				if ($row['componentType'] != 'search'){				
					if ($_body){
						$body .= $_body;
						continue;
					}
				}
				if ($row['componentType'] == 'products'){
					 foreach ($row['data'] as $keyb=>$rowb){
						 if ($rowb['visible'] == 0) continue;
						 if (empty($rowb['goodsDataType']) || $rowb['goodsDataType'] == 'custom'){
							  foreach ($rowb['products'] as $keyc=>$rowc){
								  if (!is_numeric($rowc['id'])){
									$rowb['products'][$keyc] = $d_products[$rowc['id']];
								  }else{							 
									  $grow = $GoodsModel->info($rowc['id']);
									  if(!$grow['is_on_sale']){
										 unset($rowb['products'][$keyc]);
										 continue; 
									  }
									  $rowc['thumb']['url'] = $grow['goods_thumb'];
									  $rowc['name'] = $grow['goods_name'];
									  $rowc['par_price'] = $grow['market_price'];
									  $rowc['sale_price'] = $grow['shop_price'];
									  $rowc['vip_price'] = 0;
									  $rowc['sale_count'] = $grow['sale_num']+ $grow['virtual_sale'];
									  $rowb['products'][$keyc] = $rowc;
								  }
							  }
						 }else{
							unset($where,$rowb['products']);
							$where[] = ['is_alone_sale','=',1];							
							if ($rowb['goodsDataType'] == 'recommend'){
								$where[] = ['is_best','=',1];
							}elseif ($rowb['goodsDataType'] == 'new'){
								$where[] = ['is_new','=',1];
							}else{
								$where[] = ['is_hot','=',1];
							}
							$time = time();
							$grows = $GoodsModel->field('goods_id,goods_thumb,goods_name,market_price,shop_price,is_spec,sale_num,virtual_sale')->where($where)->where("isputaway = 1 OR (isputaway = 2  AND  added_time < '".$time."' AND shelf_time > '".$time."' )")->order('update_time desc')->limit($row['dataLimit'])->select();
												
							foreach ($grows as $grow){
								$rowc['id'] = $grow['goods_id'];
								$rowc['thumb']['url'] = $grow['goods_thumb'];
								$rowc['name'] = $grow['goods_name'];
								$rowc['par_price'] = $grow['market_price'];
								$rowc['sale_price'] = $grow['shop_price'];
								$rowc['vip_price'] = 0;
								$rowc['sale_count'] = $grow['sale_num'] + $grow['virtual_sale'];
								$rowb['products'][$rowc['id']] = $rowc;
							}
						 }						
						 $row['data'][$keyb] = $rowb;
					 }
					 
				}
				
				$this->assign('_key', $_key);
				$this->assign('theme_row', $row);
				$_body = $this->fetch('page/'.$row['componentType']);
				$body .= $_body;
			}
			Cache::set($mkey,$body,60);
		}
		$this->assign('theme', $theme);
		$this->assign('body', $body);
		return $this->fetch('page/index');
	}

	public function address(){

        return $this->fetch();
    }

    /**
     *  详情
     */
    public function info(){
        $store_id = input('store_id');
        $store = Db::name('distribution_store')->where(['store_id'=>$store_id])->find();
        $shop_goods = Db::name('shop_goods')->where(['store_id'=>$store_id])->find();
        $supplyer_id = Db::name('supplyer')->where(['store_id'=>$store_id])->value('supplyer_id');
        $GoodsModel = new GoodsModel();
        $where[] = ['isputaway','=',1];
        if($supplyer_id)$where[] = ['supplyer_id','=',$supplyer_id];
        $grows = $GoodsModel->field('goods_id,goods_thumb,goods_name,market_price,shop_price,is_spec,sale_num,virtual_sale')->where($where)->order('update_time desc')->select();
//        dump($grows);exit;
//        $this->assign('allSort',$GoodsModel->getClassToAllSort());//获取分层分类
        $this->assign('classList',$grows);//获取分类
        $this->assign('store',$store);//获取分类
//        dump($GoodsModel->getClassList());exit;
        return $this->fetch();
    }
}?>